#include "purchasedingdantableview.h"
#include "readonlydelegate.h"

PurchaseDingdanTableView::PurchaseDingdanTableView(QWidget *parent) : MyTableView(parent)
{
    setSortingEnabled(true);

    model = new PurchaseDingdanStandardItemModel();
    setModel(model);

    connect(model, &PurchaseDingdanStandardItemModel::refreshTotalPlanCount,
            [=](int totalPlanCount)
    {
        emit this->refreshTotalPlanCount(totalPlanCount);
    });

    for(int i=0;i<model->header.size()-1;i++){
        setItemDelegateForColumn(i, new ReadOnlyDelegate);
    }

    connect(this, &QTableView::doubleClicked,
            [=](const QModelIndex index)
    {
        if(index.row() == model->rowCount()-1){
            productPicker.setSelectionMode(QAbstractItemView::MultiSelection);
            productPicker.showAndInit();
        }
    });

    connect(&productPicker, &ProductPicker::selectedFinished,
            [=](QList<Product*> products)
    {
        model->insertProduct(products);
    });

    setSelectionRow();
}

void PurchaseDingdanTableView::setListPushButton(ListPushButton *listPushButton){
    setListPushButtonAndMenuLabel(listPushButton, model->header);
}

void PurchaseDingdanTableView::initHiddenState(){
    initHiddenStateList("purchase/purchaseDingdan", model->header.size());
}

void PurchaseDingdanTableView::clear(){
    model->reset();
    setSelectionRow();
}

void PurchaseDingdanTableView::setSelectionRow(){
    model->appendRow(*(new QList<QStandardItem*>));
    setSpan(model->rowCount()-1, 0, 1, model->columnCount());
}

void PurchaseDingdanTableView::insertHistoryProduct(QList<HistoryProduct*> historyProducts){
    model->insertHistoryProduct(historyProducts);
}

QList<DingdanDetail*> PurchaseDingdanTableView::getDingdanDetails(){
    return model->getDingdanDetails();
}
